package cn.iocoder.yudao.module.im.controller.app.group;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.im.controller.app.group.vo.*;
import cn.iocoder.yudao.module.im.dal.dataobject.group.GroupDO;
import cn.iocoder.yudao.module.im.service.group.GroupService;

@Tag(name = "用户 APP - IM分组")
@RestController
@RequestMapping("/im/group")
@Validated
public class AppGroupController {

    @Resource
    private GroupService groupService;

    @PostMapping("/create")
    @Operation(summary = "创建IM分组")
    public CommonResult<Long> createGroup(@Valid @RequestBody AppGroupSaveReqVO createReqVO) {
        return success(groupService.createGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新IM分组")
    public CommonResult<Boolean> updateGroup(@Valid @RequestBody AppGroupSaveReqVO updateReqVO) {
        groupService.updateGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除IM分组")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteGroup(@RequestParam("id") Long id) {
        groupService.deleteGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得IM分组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppGroupRespVO> getGroup(@RequestParam("id") Long id) {
        GroupDO group = groupService.getGroup(id);
        return success(BeanUtils.toBean(group, AppGroupRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得IM分组分页")
    public CommonResult<PageResult<AppGroupRespVO>> getGroupPage(@Valid AppGroupPageReqVO pageReqVO) {
        PageResult<GroupDO> pageResult = groupService.getGroupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppGroupRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出IM分组 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportGroupExcel(@Valid AppGroupPageReqVO pageReqVO,
                                 HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GroupDO> list = groupService.getGroupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "IM分组.xls", "数据", AppGroupRespVO.class,
                BeanUtils.toBean(list, AppGroupRespVO.class));
    }

}